package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysRoleMenuService;
import com.atguigu.spzx.manager.utils.MenuHelper;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    //查询所有菜单   层级实现
    @Override
    public List<SysMenu> findNodes() {
        //查询所有菜单
        List<SysMenu> sysMenuList = sysMenuMapper.selectAll() ;
        if (CollectionUtils.isEmpty(sysMenuList)) return null;
        //利用工具类，进行数据封装
        List<SysMenu> treeList = MenuHelper.buildTree(sysMenuList); //构建树形数据
        System.out.println("Menu数据:"+treeList.toString());
        return treeList;
    }
    //解决动态添加菜单子菜单  全选默认选中的bug
    //菜单添加
    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu);
        //新添加子菜单 ，把父菜单的isHalf半开状态1
        updateSysRoleMenu(sysMenu);
    }
    //新添加子菜单 ，把父菜单的isHalf半开状态1
    private void updateSysRoleMenu(SysMenu sysMenu) {
        //获取当前添加菜单的父菜单
        SysMenu parentMenu=sysMenuMapper.selectParentMenu(sysMenu.getParentId());
        if(parentMenu!=null){
            //把父菜单的isHalf半开状态1
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId());
            //递归调用 以防有多个子菜单
            updateSysRoleMenu(parentMenu);
        }
    }

    //菜单修改
    @Override
    public void update(SysMenu sysMenu) {
        sysMenuMapper.update(sysMenu);
    }

    //菜单删除
    @Override
    public void removeById(long id) {
        //第一种  直接删除包括下面的子菜单全部删除
        //第二种  根据菜单id 查询下面的子菜单，如果有 就必须先把子菜单删除完，再删菜单
        int count=sysMenuMapper.selectCountById(id);
        //下面有子菜单 就抛出异常
        if(count>0){
            throw  new GuiguException(ResultCodeEnum.NODE_ERROR);
        }
        //count==0 就直接删除
        sysMenuMapper.removeById(id);
    }
    //查询用户可以操作的菜单
    @Override
    public List<SysMenuVo> findMenusByUserId() {
        //获取当前用户id
         SysUser sysUser = AuthContextUtil.get();
        //通过用户id 查询分配的菜单
        List<SysMenu> sysMenuList=sysMenuMapper.findMenusByUserId(sysUser.getId());
        //封装要求的数据格式返回
        final List<SysMenu> sysMenuTreeList = MenuHelper.buildTree(sysMenuList);

        return buildMenus(sysMenuTreeList);
    }
    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
